package com.itboye.zhujia.responsitory;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.reflect.TypeToken;
import com.itboye.zhujia.base.BaseNetRepository;
import com.itboye.zhujia.base.ICompleteListener;
import com.itboye.zhujia.bean.PersonDataBean;
import com.itboye.zhujia.bean.RegisterBean;
import com.itboye.zhujia.interfaces.IUserInterface;
import com.itboye.zhujia.net.BaseErrorListener;
import com.itboye.zhujia.net.BaseSuccessReqListener;
import com.itboye.zhujia.net.ByJsonRequest.Builder;

/***
 * 请求网络类
 * 
 * @author admin
 * 
 */
public class UserRepository extends BaseNetRepository implements
		IUserInterface<PersonDataBean> {
	String api = "100";
	String loginTypeKey = "BY_User_login";// 用户登录
	String sendTypeKey = "BY_Message_send_sms";// 发送短信
	String verificationTypeKey = "BY_Message_checkCode";//验证验证码
	String registerTypeKey = "BY_User_register";//注册
	String backPassTypeKey = "BY_User_findPswByMobile";//手机找回密码
	String updateTypeKey = "BY_User_update";//更新用户信息
	String UploadPicturesTypeKey = "BY_File_upload";//图片上传
	public UserRepository(ICompleteListener iCompleteListener) {
		super(iCompleteListener);
	}

	@Override
	public void login(String userName, String pwd) {
		// TODO Auto-generated method stub
		Type type = new TypeToken<PersonDataBean>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("username", userName);
		map.put("password", pwd);
		(new Builder<PersonDataBean>())
				.setTypeVerParamsAndReturnClass(loginTypeKey, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<PersonDataBean>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
	}
	/**
	 * 发送短信
	 */
	@Override
	public void send(String phone, String code) {
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", phone);
		map.put("code_type", code);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(sendTypeKey, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}
	/**
	 * 验证验证码
	 */
	@Override
	public void verification(String mobile, String code_type, String code) {
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", mobile);
		map.put("code_type", code_type);
		map.put("code", code);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(verificationTypeKey, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}
	/**
	 * 注册
	 */
	@Override
	public void register(String reg_type, String from, String code,
			String username, String password) {
		Type type = new TypeToken<RegisterBean>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("reg_type", reg_type);
		map.put("from", from);
		map.put("code", code);
		map.put("username", username);
		map.put("password", password);
		(new Builder<RegisterBean>())
				.setTypeVerParamsAndReturnClass(registerTypeKey, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<RegisterBean>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}
	/**
	 * 手机找回密码
	 */
	@Override
	public void backPass(String mobile, String code, String psw) {
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", mobile);
		map.put("code", code);
		map.put("psw", psw);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(backPassTypeKey, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}
	/**
	 * 修改性别
	 */
	@Override
	public void updateSex(String uid, String sex) {
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("sex", sex);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(updateTypeKey, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}
	/**
	 * 修改昵称
	 */
	@Override
	public void updateNickname(String uid, String nickname) {
		Type type = new TypeToken<String>() {
		}.getType();
		String apiVer = api;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("nickname", nickname);
		(new Builder<String>())
				.setTypeVerParamsAndReturnClass(updateTypeKey, apiVer, map, type)
				.requestListener(
						new BaseSuccessReqListener<String>(
								getListener()))
				.errorListener(new BaseErrorListener(getListener()))
				.desEncodeThenBuildAndSend();
		
	}
	/**
	 * 修改头像
	 */
	@Override
	public void updateHead(String uid, String type,String image) {
		Type type01=new TypeToken<String>(){}.getType();
		
		String apiver=api;
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("uid", uid);
		map.put("type", type);
		map.put("image", image);
		(new Builder<String>()).setTypeVerParamsAndReturnClass(UploadPicturesTypeKey, apiver, map, type01)
		.requestListener(new BaseSuccessReqListener<String>(getListener()))
		.errorListener(new BaseErrorListener(getListener()))
		.desEncodeThenBuildAndSend();
		
	}

}
